package com.matrix.draft.rabbitmqspringboot.config.after;

import cn.hutool.core.lang.Console;
import com.matrix.draft.rabbitmqspringboot.rabbitmq.callback.AdvanceConfirmCallback;
import com.matrix.draft.rabbitmqspringboot.rabbitmq.callback.AdvanceReturnedCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 当Spring加载完成之后, 可能要做一些操作
 * Spring会调用这个ApplicationRunner
 */
@Component
public class GlobalApplicationRunner implements ApplicationRunner {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    AdvanceConfirmCallback confirmCallback;

    @Autowired
    AdvanceReturnedCallback returnedCallback;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化RabbitMQ发布确认回调
        rabbitTemplate.setConfirmCallback(confirmCallback);
        Console.log("RabbitMQ发布确认回调函数完成");
        rabbitTemplate.setReturnCallback(returnedCallback);
        Console.log("RabbitMQ消息回退回调函数完成");
    }

}
